/************************************************************************

								Heroes IV
					  Copyright 2001, The 3DO Company

	------------------------------------------------------------------
  								adv_sign.cpp

	$Header:  $

	$NoKeywords: $

 ************************************************************************/


// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_sign.h"

#include "adv_object_type.h"
#include "adventure_map.h"
#include "adventure_sounds.h"
#include "army.h"
#include "basic_dialog.h"
#include "external_string.h"
#include "mouse_event.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "simple_dialog.h"

namespace
{
	int const k_current_map_format_version = 1;

	int const k_current_save_format_version = 2;

	t_object_registration<t_adv_sign> k_registration( k_adv_object_sign );

	t_external_string const k_blank_message("blank_message.misc");
}

t_adv_sign::t_adv_sign( std::string const& model_name )
          : t_stationary_adventure_object( model_name), m_message(k_blank_message)
{
}

void t_adv_sign::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                           t_direction direction, t_adventure_frame* frame )
{
	if ( army->get_owner() == NULL || army->get_owner()->is_computer() )
		return;

    t_counted_ptr<t_basic_dialog> message_dialog_ptr = new t_basic_dialog;

	m_visited[army->get_owner_number()] = true;
    // Get text from map...
    message_dialog_ptr->set_text( m_message, k_justify_left );

    message_dialog_ptr->add_ok_button();
    message_dialog_ptr->open();
	t_sound_cache music = get_music_playing();
	stop_music();
	get_dialog_sound( k_dialog_sound_sign )->play( get_sound_volume() );
    message_dialog_ptr->run_modal();
	play_music( music );
}

int t_adv_sign::get_version() const
{
	return k_current_save_format_version;
}

bool t_adv_sign::read_from_map(
						std::streambuf&		buffer,
						t_progress_handler* progress)
{
	int version = get< t_uint16 >( buffer );
	if ( version < 0 || version > k_current_map_format_version )
		return false;

	if (version >= 1)
	{
		::read_string16(buffer, m_message);
	}

	return true;
}

bool t_adv_sign::read( std::streambuf&						stream, 
					   t_qualified_adv_object_type const&	type,
					   int									version )
{
	if (version >= 1)
	{
		::read_string16(stream, m_message);
	}
	if (version >= 2)
		m_visited.read( stream );

	return true;
}

bool t_adv_sign::write( std::streambuf& stream ) const
{
	::write_string16(stream, m_message);
	m_visited.write( stream );
	return true;
}

void t_adv_sign::right_click( t_mouse_event const& event,
							  t_adventure_frame* adventure_frame )
{
	t_skill_mastery information = get_information_level();
	int				player = get_map()->get_player_number();

	if (information >= k_mastery_basic)
		m_visited[player] = true;
	if (!m_visited[player])
	{
		t_stationary_adventure_object::right_click( event, adventure_frame );
		return;
	}
	show_popup_text( m_message, event.screen_point );
}
